﻿using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DXTools.UI.Modules.MetadataBrowser.Model;
using DXTools.Core;
using DXTools.Core.CrmSdk;
using DXTools.UI.Core.ViewModel;
using DXTools.UI.Modules.MetadataBrowser.ViewModels;
using System.Collections.ObjectModel;

namespace DXTools.UI.Modules.MetadataBrowser.DataProviders
{
   
    public class CRMDataProvider : IDataProvider
    {
        Uri _server;

        public CRMDataProvider()
        {
            _server = new Uri(DXTools.Core.ClientScript.GetServerUrl());
        }


        public void RetrieveEntities(AsyncCallback asyncCallback)
        {
            try
            {
                DXTools.Core.CrmSdk.IOrganizationService service = DXTools.Core.WCFHelper.GetConnection(_server);
               
                OrganizationRequest request = new OrganizationRequest();
                request.RequestName = "RetrieveAllEntities";
                request.Parameters = new ParameterCollection();
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("EntityFilters", EntityFilters.Entity));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("RetrieveAsIfPublished", false));

                service.BeginExecute(request, asyncCallback, service);

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw ex;
            }
        }


        public void RetrieveEntityAttributes(String EntityLogicalName, AsyncCallback asyncCallback)
        {
            try
            {
                DXTools.Core.CrmSdk.IOrganizationService service = DXTools.Core.WCFHelper.GetConnection(_server);
               
                OrganizationRequest request = new OrganizationRequest();
                request.RequestName = "RetrieveEntity";
                request.Parameters = new ParameterCollection();
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("LogicalName", EntityLogicalName));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("EntityFilters", EntityFilters.Attributes));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("MetadataId", new Guid("00000000-0000-0000-0000-000000000000")));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("RetrieveAsIfPublished", false));

                service.BeginExecute(request, asyncCallback, service);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw ex;
            }
        }


        public void RetrieveEntityRelationships(String EntityLogicalName, AsyncCallback asyncCallback)
        {
            try
            {
                DXTools.Core.CrmSdk.IOrganizationService service = DXTools.Core.WCFHelper.GetConnection(_server);

                OrganizationRequest request = new OrganizationRequest();
                request.RequestName = "RetrieveEntity";
                request.Parameters = new ParameterCollection();
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("LogicalName", EntityLogicalName));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("EntityFilters", EntityFilters.Relationships));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("MetadataId", new Guid("00000000-0000-0000-0000-000000000000")));
                request.Parameters.Add(new DXTools.Core.CrmSdk.KeyValuePair<string, object>("RetrieveAsIfPublished", false));

                service.BeginExecute(request, asyncCallback, service);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw ex;
            }
        }
       
    }
}
